<?php
/*********************************************************
author			Henson
date			2011-6-14
function		评论统计
*********************************************************/
set_time_limit(0);
include_once( _INC_PATH."database.php" );

class Statistic{
	var $cor_id = 0;
	function statistic($cor_id=0)
	{
		if($cor_id > 0){
			$this->cor_id = $cor_id;
		}
	}
	function updateStatistic()
	{
		$cor_id = $this->cor_id;
		$db = new database();
		if($cor_id > 0){
			$select_where = " where tc.id='$cor_id'";
		}
		$sql = "select IFNULL(st.total,0)as total,
			IFNULL(st.sal_sum,0)as sal_sum,
			IFNULL(st.enr_sum,0)as enr_sum,
			IFNULL(st.opp_sum,0)as opp_sum,
			IFNULL(st.pre_sum,0)as pre_sum,tc.id as cor_id
		from tbl_corporation tc left join(
			select count(id) as total,sum(salary) as sal_sum,
			sum(environment) as enr_sum,sum(opportunity) as opp_sum,
			sum(pressure) as pre_sum,cor_id from tbl_estimate 
			where del_flg='0' and show_flg='1' group by cor_id) st on st.cor_id=tc.id $select_where";
		$db->setQuery($sql);
		$rows = $db->loadList();
		foreach($rows as $key=>$data)
		{
			$t_score = intval($data['total'] * 4);
			$row = array();
			$row['cor_id'] = $data['cor_id'];
			$row['estimate_total'] = intval($data['total']);
			$row['sal_per'] = $t_score > 0 ? round($data['sal_sum'] / $t_score * 100) : 0;
			$row['enr_per'] = $t_score > 0 ? round($data['enr_sum'] / $t_score * 100) : 0;
			$row['opp_per'] = $t_score > 0 ? round($data['opp_sum'] / $t_score * 100) : 0;
			$row['pre_per'] = $t_score > 0 ? round($data['pre_sum'] / $t_score * 100) : 0;
			$row['update_time'] = date("Y-m-d H:i:s");
			$sql = "select cor_id from tbl_statistic where cor_id='".$data['cor_id']."'";

			if($db->loadResult($sql)){
				$where = " where cor_id='".$data['cor_id']."'";
				$db->update($row,"tbl_statistic",$where);
			}else{
				$row['create_time'] = date("Y-m-d H:i:s");
				$db->insert($row,"tbl_statistic");
			}
		}
	}
}
?>